<?xml version="1.0" encoding="UTF-8" ?>

<!--
    Geocache: some utilities for managing and visualizing geocache information
    Copyright (C) 2008  Gary Jackson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  -->

<xsl:stylesheet version="2.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:groundspeak="http://www.groundspeak.com/cache/1/0"
		xmlns:gpx="http://www.topografix.com/GPX/1/0"
		xmlns="http://www.topografix.com/GPX/1/0">
  <xsl:param name="name" />
  <xsl:param name="author"/>
  <xsl:param name="email"/>
  <xsl:param name="nameLen">14</xsl:param>
  <xsl:param name="descrLen">88</xsl:param>

  <xsl:variable xmlns:digest="java:com.thegbomb.geocache.SmartName"
		name="smartName"
		select="digest:new($nameLen)"/>

  <xsl:output method="xml" version="1.0"
	      encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="gpx:gpx"/>
  </xsl:template>

  <xsl:template match="gpx:gpx">
    <gpx>
      <name><xsl:value-of select="$name"/></name>
      <desc>
	Geocache File transformed from Groundspeak sources by Haymaker
      </desc>
      <author><xsl:value-of select="$author"/></author>
      <email><xsl:value-of select="$email"/></email>
      <time><xsl:value-of select="current-dateTime()"/></time>
      <xsl:copy-of select="gpx:bounds"/>
      <xsl:apply-templates select="gpx:wpt"/>
    </gpx>
  </xsl:template>

  <xsl:template match="gpx:wpt">
    <wpt>
      <xsl:attribute name="lat">
	<xsl:value-of select="@lat"/>
      </xsl:attribute>
      <xsl:attribute name="lon">
	<xsl:value-of select="@lon"/>
      </xsl:attribute>
      <name>
	<xsl:value-of select="groundspeak:cache/groundspeak:difficulty * 2 - 1"/>
	<xsl:value-of select="groundspeak:cache/groundspeak:terrain * 2 - 1"/>
	<xsl:value-of select="substring(groundspeak:cache/groundspeak:type, 1, 1)"/>
	<xsl:value-of select="substring(groundspeak:cache/groundspeak:container, 1, 1)"/>
	<xsl:value-of xmlns:digest="java:com.thegbomb.geocache.SmartName"
		      select="digest:convert($smartName, gpx:name, groundspeak:cache/groundspeak:name)"/>
      </name>
      <desc>
	<xsl:value-of select="gpx:name"/>;<xsl:value-of select="replace(groundspeak:cache/groundspeak:encoded_hints,'[\n\r]','')"/>;<xsl:apply-templates select="groundspeak:cache/groundspeak:logs/groundspeak:log"/>
      </desc>
    </wpt>
  </xsl:template>

  <xsl:template match="groundspeak:log">
    <xsl:value-of select="substring(groundspeak:type, 1, 1)"/>
  </xsl:template>
</xsl:stylesheet>
